package ywpt.service.contract;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import ywpt.core.dao.HibernateEntityDao;
import ywpt.model.ContractType;
import ywpt.model.PaymentContract;
import ywpt.model.ProviderInfo;
import ywpt.web.common.PublicFunction;

public class PaymentContractServiceImpl extends HibernateEntityDao<PaymentContract> implements PaymentContractService {
	public PaymentContractServiceImpl(){
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<PaymentContract> getPaymentContracts(DetachedCriteria query,
			int pageSize, int startIndex) {
		return this.findAllByCriteria(query, pageSize, startIndex);
	}

	@Override
	public int getPaymentContractsCountByCriteria(DetachedCriteria query) {
		return this.getCountByCriteria(query);
	}

	@Override
	public String createPaymentCode() {
		String str = "WXP";
		SimpleDateFormat FORMAT = new SimpleDateFormat("yyyyMM");
		DetachedCriteria query = DetachedCriteria.forClass(PaymentContract.class,"paymentContract");
		query.add(Restrictions.like("paymentCode",str+FORMAT.format(new Date())+"%"));
		String paymentCode="";
		String code="";
		Integer maxId=new Integer(0);
		query.addOrder(Order.desc("id"));
		List<PaymentContract> paymentContracts = this.getPaymentContracts(query);
		if ( paymentContracts.iterator().hasNext() )	
		{
			code = (paymentContracts.iterator().next().getPaymentCode()).substring(9);
			maxId = new Integer(code);
		}
		paymentCode = PublicFunction.getNextCode("", maxId, false,3); 
		query.setProjection(null);
		return str+FORMAT.format(new Date())+paymentCode;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<PaymentContract> getPaymentTypes(DetachedCriteria query) {
		return this.findAllByCriteria(query);
	}

	@Override
	public PaymentContract getPaymentContractById(Long id) {
		return this.get(id);
	}

	@Override
	public void savePaymentContract(PaymentContract paymentContract,Long typeId, Long providerId) {
		if(providerId!=null)
		{
			paymentContract.setProvider(this.get(ProviderInfo.class, providerId));
		}
		if(typeId != null ){
			paymentContract.setType(this.get(ContractType.class, typeId));
		}
		super.save(paymentContract);
	}

	@Override
	public void updatePaymentContract(PaymentContract paymentContract,Long typeId, Long providerId) {
		if(providerId!=null)
		{
			paymentContract.setProvider(this.get(ProviderInfo.class, providerId));
		}
		if(typeId != null ){
			paymentContract.setType(this.get(ContractType.class, typeId));
		}
		this.update(paymentContract);
	}

	@Override
	public void removePaymentContract(Long id) {
		this.removeById(id);
	}

	@Override
	public List<PaymentContract> getPaymentContracts() {
		return this.getAll();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ContractType> getContractTypes() {
		String hql="select distinct(type) from PaymentContract";
		return this.find(hql);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<PaymentContract> getPaymentContracts(DetachedCriteria query) {
		return this.findAllByCriteria(query);
	}
}
